#include"Sort.h"
Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void ArrPrint(int* arr, int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		printf("%d", arr[i]);
	}
	printf("\n");
}
int midkey(int* arr, int left, int right)
{
	int mid = (left + right) / 2;
	if (arr[left] > arr[right])
	{
		if (arr[right] > mid)
		{
			return right;
		}
		else if (arr[left] > arr[mid])
		{
			return mid;
		}
		else
		{
			return left;
		}
	}
	else //arr[left] < arr[right]
	{
		if (arr[right] < arr[mid])
		{
			return right;
		}
		else if (arr[left] < arr[mid])
		{
			return mid;
		}
		else
		{
			return left;
		}
	}
}
int SortKey1(int* arr, int left, int right)
{
	int keyi = left;
	int n = midkey(arr, left, right);
	Swap(&arr[keyi], &arr[n]);
	while (left < right)
	{
		while (right > left && arr[right] >= arr[keyi])
		{
			right--;
		}
		arr[left] = arr[right];

		while (right > left && arr[left] <= arr[keyi])
		{
			left++;
		}
		arr[right] = arr[left];
	}
	arr[left] = keyi;
	return left;
}
int SortKey2(int* arr, int left, int right)
{
	int keyi = left;
	int n = midkey(arr, left, right);
	Swap(&arr[keyi], &arr[n]);
	int prev = left;
	int cut = left;
	while (cut <= right)
	{
		if (arr[cut] < arr[keyi])
		{
			prev++;
			Swap(&arr[cut], &arr[prev]);
		}
		cut++;
	}
	Swap(&arr[keyi], &arr[prev]);
	return prev;
}
void _IncoSort()
{

}
IncoSort()
{

}
void QuickSort(int* arr, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int key = SortKey2(arr, begin, end);

	QuickSort(arr, begin, key - 1);
	QuickSort(arr, key + 1, end);
}